﻿using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;
using System.IO;

namespace Chat_WebApi.ConfigServices
{
    public static class SwaggerService
    {
        /// <summary>
        /// 添加 swagger
        /// </summary>
        /// <param name="services"></param>
        public static void AddSwaggerService(this IServiceCollection services)
        {
            if (services == null)
            {
                throw new System.Exception();
            }

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo
                {
                    Version = "v1",
                    Title = "Api Swagger",
                    Description = "聊天",
                });

                // 加载程序集的xml描述文档
                var baseDirectory = System.AppDomain.CurrentDomain.BaseDirectory;

                var xmlFile = System.AppDomain.CurrentDomain.FriendlyName + ".xml";
                var xmlPath = Path.Combine(baseDirectory, xmlFile);
                c.IncludeXmlComments(xmlPath, true);
                c.OrderActionsBy(o => o.RelativePath);

                var xmlModelFile = "Chat_Model.xml";
                var xmlModelPath = Path.Combine(baseDirectory, xmlModelFile);
                c.IncludeXmlComments(xmlModelPath);
            });
        }
    }
}
